<?php
// 引用数据库连接文件
require_once 'databaseConnection.php';

// 连接数据库
try {
    $dbConn = connectToDatabase();
    // echo "成功连接到数据库";
    // 接收通过Ajax发送的表单数据
    $roomNumber = $_POST['roomNumber'] ?? '';
    $roomType = $_POST['roomType'] ?? '';
    $status = $_POST['status'] ?? '';
    $cleanliness = $_POST['cleanliness'] ?? null;
    $cleaner = $_POST['cleaner'] ?? null;
    $qualified = $_POST['qualified'] ?? null;
    $sellable = $_POST['sellable'] ?? null;
    $lastCleaned = $_POST['lastCleaned'] ?? null;
    // 在此处进行处理，可以将数据存储到数据库
    $stmt = $dbConn->prepare("INSERT INTO roomstatus (number, roomType, status, cleanliness, cleaner, qualified, sellable, lastCleaned) VALUES (:roomNumber, :roomType, :status, :cleanliness, :cleaner, :qualified, :sellable, :lastCleaned)");
    $stmt->bindParam(':roomNumber', $roomNumber);
    $stmt->bindParam(':roomType', $roomType);
    $stmt->bindParam(':status', $status);
    $stmt->bindParam(':cleanliness', $cleanliness);
    $stmt->bindParam(':cleaner', $cleaner);
    $stmt->bindParam(':qualified', $qualified);
    $stmt->bindParam(':sellable', $sellable);
    $stmt->bindParam(':lastCleaned', $lastCleaned);
    $stmt->execute();
    // 获取新插入记录的ID
    $newRecordId = $dbConn->lastInsertId();
    // 处理成功后返回响应
    $responseData = array(
        'message' => '数据已成功处理',
        'newRecordId' => $newRecordId
    );
    // 设置响应头为 JSON
    header('Content-Type: application/json');
    // 将响应数据转换为 JSON 格式并输出
    echo json_encode($responseData);
    // 关闭数据库连接
    $dbConn = null;
} catch (PDOException $e) {
    echo "连接数据库时发生错误: " . $e->getMessage();
}
